﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.DataAccess.Base;
using Legazpi.Payroll.DataAccess.Repositories;


namespace Legazpi.Payroll.BusinessLogic.Services
{
    public class PositionService : IPositionService
    {

        #region Fields

        private DbContext _dbContext;
        private IPositionRepository _positionRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public PositionService()
            : this(new DatabaseFactory())
        {

        }

        public PositionService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _positionRepository = new PositionRepository(_dbContext);
            }
        }

        #endregion

        public Position GetById(int id)
        {
            return _positionRepository.GetById(id);
        }

        public IEnumerable<Position> GetAll()
        {
            return _positionRepository.GetAll();
        }

        public IEnumerable<Position> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(Position position)
        {
            _positionRepository.Add(position);
        }

        public void Update(Position position)
        {
            _positionRepository.Update(position);
        }

        public void Delete(int id)
        {
            var position = GetById(id);
            if (position != null)
                _positionRepository.Delete(position);
        }

        public bool Save()
        {
            string message;
            _positionRepository.Save(out message);
            return message == "Saved";
        }
    }
}
